package com.bidevalution.business.function;

import java.util.Map;
import com.googlecode.aviator.runtime.function.AbstractFunction;
import com.googlecode.aviator.runtime.function.FunctionUtils;
import com.googlecode.aviator.runtime.type.AviatorObject;
import com.googlecode.aviator.runtime.type.AviatorString;
import com.qtp.core.util.GsonUtil;
/**
 * 从list(map)中获取某个key的值，以逗号隔开，是否添加引号
 * @author daizhifeng   
 * @date 2019年10月10日 下午9:41:11 
 * @version V2.0
 */
public class GetObjectOfListFunction extends AbstractFunction{
	public GetObjectOfListFunction(FunctionInitializer functionInitializer) {
		super();
	}
	/**
	 *  从list(map)中获取某个key的值，以逗号隔开，是否添加引号
	 * <p>Description: </p>
	 * @param env
	 * @param arg1 list(map)值
	 * @param arg2 map对应的key
	 * @param arg3 单引号或者其他符号
	 * @return 例如:'2','3','4'
	 * @see com.googlecode.aviator.runtime.function.AbstractFunction#call(java.util.Map, com.googlecode.aviator.runtime.type.AviatorObject, com.googlecode.aviator.runtime.type.AviatorObject, com.googlecode.aviator.runtime.type.AviatorObject)
	 */
	@Override
	public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3) {
		String paramValue = FunctionUtils.getStringValue(arg1, env);
		if(paramValue != null && paramValue.length()>0) {
			String key = arg2.getValue(env).toString();
			String quote = arg3.getValue(env).toString();
			StringBuilder stringBuilder = new StringBuilder();
			for(Map<String, Object> dataMap:GsonUtil.jsonToListMapWithGson(paramValue)) {
				stringBuilder.append(quote).append(dataMap.get(key)).append(quote).append(",");
			}
			if(stringBuilder.length()>0) {
				return new AviatorString(stringBuilder.subSequence(0, stringBuilder.length() - 1).toString());
			}
		}
		return new AviatorString("");
	}
	@Override
	public String getName() {
		return "getObjectOfList";
	}

}
